﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Web.Profile;
using IronOne.SalesOne.BusinessObjects;
using System.Configuration;
using Facade;
using System.Collections;
using System.Data;


namespace IronOne.SalesOne.SalesOneWeb.ContactManagement
{
    public partial class ViewDependent : SOSecurityPage, IDataPage, IntTest
    {
        private int pageNo = 1;

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {

                if (!IsPostBack)
                {
                    ViewState["refUrl"] = Request.UrlReferrer.ToString();
                    ValidateUserCredentials(Request.Url.LocalPath);

                    UCGrid1.styleList = new List<GridViewStyle> {
                                new GridViewStyle{
                                    ColumnId = "Name",
                                    ColumnHeader = "Name",
                                    ColumnWidth = 130 },
                                new GridViewStyle{
                                    ColumnId = "DependentType",
                                    ColumnHeader = "Dependent Type",
                                    ColumnWidth = 130},
                               new GridViewStyle{
                                    ColumnId = "DOB",
                                    ColumnHeader = "Date of Birth",
                                    ColumnWidth = 130,
                                    Align = "right"}
                                
                            };

                    pageNo = 1;
                    LoadDepGrid(1);
                    UCGrid1.SetText("1");
                }
            }
            catch (Exception)
            {

            }


            //string[] but = new string[] { "btnView", "btnCancel" };
            //UCGridButtons1.Buttons = but;

        }



        public void LoadDepGrid(int pageNo)
        {
            try
            {
                CustomerFacade cFac = new CustomerFacade();

                if (!string.IsNullOrEmpty(Request.QueryString["nid"]))
                {
                    int nid = Convert.ToInt32(Request.QueryString["nid"]);
                    List<NaDependent> ndep = cFac.GetDependentsForNa(nid, pageNo);


                    int[] numbers = { 1, 2, 3, 4, 7, 9 };
                    UCGrid1.SetDataSource(ndep, numbers);

                    if (ndep.Count > 0)
                    {
                        UCGridButtons1.Visible = true;
                        UCGridButtons1.SetCredentials(ValidateUserCredentials(Request.Url.LocalPath));

                    }
                    else
                    {
                        MassageSummery("IIS504");
                        //lblMessage.Text = "No dependants to display";
                        //lblMessage.ForeColor = System.Drawing.Color.Red;
                        UCGridButtons1.Visible = false;
                    }
                }
                else
                {
                    if (Session["prospectId"] != null && Session["cusId"] != null)
                    {
                        string custId = Session["cusId"].ToString();


                        List<Dependant> dep = cFac.GetDependentsForCustomer(custId, pageNo);

                        int[] numbers = { 1, 2, 3, 6, 8 };
                        UCGrid1.SetDataSource(dep, numbers);

                        if (dep.Count > 0)
                        {
                            UCGridButtons1.Visible = true;
                            UCGridButtons1.SetCredentials(ValidateUserCredentials(Request.Url.LocalPath));

                        }
                        else
                        {
                            MassageSummery("IIS504");
                            //lblMessage.Text = "No dependants to display";
                            //lblMessage.ForeColor = System.Drawing.Color.Red;
                            UCGridButtons1.Visible = false;
                        }


                    }
                }
            }
            catch (Exception)
            {
                
            }

        }

        #region IDataPage Members

        public void Create()
        {

        }

        public void View()
        {
            if (Session["dependant"] == null)
            {

                Session["dependant"] = ((GridViewRow)Session["firstRow"]);
                Session.Remove("firstRow");
            }

            if (!string.IsNullOrEmpty(Request.QueryString["nid"]))
            {
                Response.Redirect("~/ContactManagement/DependantDetails.aspx?nid=" + Request.QueryString["nid"]);
            }
            else
            {
                Response.Redirect("~/ContactManagement/DependantDetails.aspx");
            }
            
            //Response.Redirect("~/ContactManagement/DependantDetails.aspx");
            //Response.Redirect("www.google.com");
        }

        public void Update()
        {

        }

        public void Delete()
        {

        }

        public void Cancel()
        {

        }

        public void GetId(GridViewRow row)
        {
            Session["dependant"] = row;
        }

        #endregion

        #region IntTest Members

        public void Next()
        {
            try
            {
                if (UCGrid1.GetText() != "1")
                {
                    pageNo = Convert.ToInt32(UCGrid1.GetText()) + 1;
                    UCGrid1.SetText(pageNo.ToString());

                }
                else
                {
                    pageNo = 2;
                    UCGrid1.SetText("2");

                }

                LoadDepGrid(pageNo);
            }
            catch (Exception)
            {

            }
            

        }

        public void Previous()
        {
            try
            {
                if (UCGrid1.GetText() != "1")
                {
                    pageNo = Convert.ToInt32(UCGrid1.GetText()) - 1;
                    UCGrid1.SetText(pageNo.ToString());

                    LoadDepGrid(pageNo);
                }

            }
            catch (Exception)
            {

            }
            

        }

        public void Go()
        {
            try
            {
                int p = int.Parse(UCGrid1.GetText());
                if (p != 0)
                {
                    try
                    {
                        LoadDepGrid(p);
                        pageNo = p;
                    }
                    catch (Exception)
                    {

                        throw;
                    }


                }
            }
            catch (Exception)
            {

            }
            

        }

        #endregion

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(Request.QueryString["nid"]))
                {
                    int nid = Convert.ToInt32(Request.QueryString["nid"]);
                    string ac = Request.QueryString["a"];

                    CustomerFacade cFac = new CustomerFacade();

                    SalesOne.BusinessObjects.NeedsAnalysis nad = cFac.GetNeedAnalysis(nid);

                    Response.Redirect("~/ContactManagement/ProspectDetails.aspx?id=" + nad.ProspectId + "&t=v&nid=" + nad.NeedAnalysisNo);
                }
                else
                {
                        Response.Redirect("~/ContactManagement/ProspectDetails.aspx?id=" + Session["prospectId"].ToString());

                }
            }
            catch (Exception)
            {

            }
        }
    }
}
